package com.ji.li.reactadminproexampleservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.ji.li.reactadminproexampleservice.dto.UserLogin;
import com.ji.li.reactadminproexampleservice.entity.http.ResEntity;
import com.ji.li.reactadminproexampleservice.entity.http.ResEnum;
import com.ji.li.reactadminproexampleservice.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author ji.li
 * react-admin-pro-example-service
 * 唯一 controller
 */
@Validated
@RestController
@RequestMapping("/api")
public class ApiController {
    @Autowired
    private final ApiService apiService;

    public ApiController(ApiService apiService) {
        this.apiService = apiService;
    }

    @ApiOperation("账号登录")
    @PostMapping("/public/login/account")
    public ResponseEntity<ResEntity> adminLoginAccount(
            @Valid @RequestBody UserLogin dto) {
        return apiService.adminLoginAccount(dto);
    }

    @ApiOperation("获取当前用户")
    @GetMapping("private/getCurrentUser")
    public ResponseEntity<ResEntity> getCurrentUser() {
        return apiService.getCurrentUser();
    }

    @ApiOperation("账号登出")
    @PostMapping("/private/loginOut")
    public ResponseEntity<Void> adminLoginOut() {
        apiService.adminLoginOut();
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("手机号登录")
    @PostMapping("/public/login/phone")
    public void adminLoginPhone() {
    }

    @ApiOperation("获取用户列表(权限:ADMIN)")
    @GetMapping("/private/users")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public ResponseEntity<ResEntity> users() {
        System.out.println("是否走到了 controller users");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sss","llll");
        return ResponseEntity.ok().body(new ResEntity(ResEnum.SUCCESS,jsonObject));
    }

    @ApiOperation("根据用户权限,获取导航菜单(权限:token)")
    @GetMapping("/private/getNavEnum")
    public ResponseEntity<ResEntity> getNavEnum() {
        return null;
    }
}
